import 'package:common_utils/common_utils.dart';
import 'package:x_core/x_core.dart';

/// Created by XieXin on 2020/7/28.
/// 账号工具类
class AccountUtils {
  static const String isLoginKey = "account_is_login";
  static const String usernameKey = "account_username";

  ///是否用户登录
  static bool? _isLogin;

  ///手机号
  static String? _username;

  ///授权Token
  static String? _token;

  static bool get isLogin {
    return _isLogin ??= SPUtils.getBool(isLoginKey);
  }

  static set isLogin(bool value) {
    _isLogin = value;
    SPUtils.putBool(isLoginKey, value);
  }

  static String get username {
    if (TextUtil.isEmpty(_username)) _username = SPUtils.getString(usernameKey);
    return _username ??= SPUtils.getString(usernameKey);
  }

  static set username(String value) {
    _username = value;
    SPUtils.putString(usernameKey, value);
  }

  static String get token {
    if (TextUtil.isEmpty(_token)) _token = SPUtils.getString(XConstant.token);
    return _token ??= SPUtils.getString(XConstant.token);
  }

  static set token(String value) {
    _token = value;
    SPUtils.putString(XConstant.token, value);
  }

  static Future<bool> logout() async {
    bool isClear = await SPUtils.remove(isLoginKey);
    isClear = await SPUtils.remove(XConstant.token);
    if (isClear) {
      _isLogin = false;
    }
    return isClear;
  }
}
